package hl.demosample.app.ext

import androidx.appcompat.widget.Toolbar
import hl.demosample.R
import hl.demosample.app.util.SettingUtil
import hl.demosample.app.weight.toolbar.CustomToolBar
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.util.toHtml

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-01 15:01
* Changes (from 2021-06-01)
* Desc:
*/
/**
 * 初始化普通的toolbar 只设置标题
 */
fun Toolbar.init(titleStr: String = ""): Toolbar {
    setBackgroundColor(SettingUtil.getColor(appContext))
    title = titleStr
    return this
}

/**
 * 初始化有返回键的toolbar
 */
fun Toolbar.initClose(
    titleStr: String = "",
    backImg: Int = R.drawable.ic_back,
    onBack: (toolbar: Toolbar) -> Unit
): Toolbar {
    setBackgroundColor(SettingUtil.getColor(appContext))
    title = titleStr.toHtml()
    setNavigationIcon(backImg)
    setNavigationOnClickListener { onBack.invoke(this) }
    return this
}

/**
 * 初始化有返回键的toolbar
 * 标题居中
 */
fun CustomToolBar.initBack(
    titleStr: String = "",
    backImg: Int = R.drawable.ic_back,
    onBack: (toolbar: CustomToolBar) -> Unit
): CustomToolBar {
    this.setBackgroundColor(SettingUtil.getColor(appContext))
    this.setCenterTitle(titleStr)
    this.getBaseToolBar().setNavigationIcon(backImg)
    this.getBaseToolBar().setNavigationOnClickListener { onBack.invoke(this) }
    return this
}